package com.sxbbc.common.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.function.common.GetRest;
import com.function.common.RestResponse;
import com.sxbbc.common.basics.exceptions.ThrowJsonException;
import com.sxbbc.common.core.entity.jz.CFootprintInfo;
import com.sxbbc.common.core.entity.jz.ProcMain;
import com.sxbbc.common.core.mapper.jz.CFootprintInfoMapper;
import com.sxbbc.common.core.service.ICFootprintInfoService;
import com.sxbbc.common.core.utils.BasePage;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 用户浏览足迹 服务实现类
 * </p>
 *
 * @author leehao
 * @since 2019-05-16
 */
@Service
public class CFootprintInfoServiceImpl extends ServiceImpl<CFootprintInfoMapper, CFootprintInfo> implements ICFootprintInfoService {

    @Autowired
    private CFootprintInfoMapper footprintInfoMapper;

    @Override
    public IPage<CFootprintInfo> queryCustomerFootList(CFootprintInfo foot, BasePage<CFootprintInfo> page) {
        IPage<CFootprintInfo> timeList = footprintInfoMapper.queryFootPrintGroupByTime(page,foot.getCid());
        for (CFootprintInfo c : timeList.getRecords()) {
            c.setCid(foot.getCid());
            List<CFootprintInfo> footList = footprintInfoMapper.queryFootPrintByTime(c);
            c.setFootList(footList);
        }
        return timeList;
    }

    @Override
    public RestResponse addFootprint(ProcMain procMain) {
        boolean save = false;
        if (procMain.getCid() != null && StringUtils.isNotBlank(procMain.getProCode())) {
            CFootprintInfo info = new CFootprintInfo();
            info.setBid(procMain.getBid());
            info.setCid(procMain.getCid());
            info.setProId(procMain.getId());
            info.setProCode(procMain.getProCode());
            info.setProName(procMain.getProName());
            info.setProPrice(procMain.getPrice());
            info.setProUrl(procMain.getProUrl());
            info.setDescVal(procMain.getProDesc());
            info.setProUnit(procMain.getProUnit());
            QueryWrapper<CFootprintInfo> wrapper = new QueryWrapper<>();
            wrapper.eq("cid",procMain.getCid()).eq("pro_code",procMain.getProCode());
            CFootprintInfo one = super.getOne(wrapper);
            if (one != null) {
            	info.setCreateTime(new Date());
                info.setId(one.getId());
                save = super.updateById(info);
            }else{
                save = super.save(info);
            }
            if (!save) {
                throw new ThrowJsonException("保存足迹失败");
            }
        }
        return GetRest.getSuccess("");
    }


    /**
     * 查询用户足迹中最多的二级分类id
     * @param cid
     * @return
     */
    @Override
    public Integer queryLastSecondClassify(Integer cid) {
        return footprintInfoMapper.getMaxNumSecondIdByCid(cid);
    }
}
